//语种识别服务,翻译服务等


var getSourceURL = httphostname+"/plugin/lang";//语种判断接口
var transURL = httphostname+"/plugin/translate";//翻译接口


/**
 * 监听来自content script 的动作请求
 */
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
	

	var action = request.action;

	if(action == "lang_type_action"){//语种识别请求
		sendResponse({"lang_type":get_lang_type(request)});
	}
	else if(action == "translate_action"){//划词翻译服务请求
		sendResponse({"text":translate(request)});
	}
	else if(action == "list_translate_action"){//网页翻译服务请求
		sendResponse({"data":list_translate(request)});
	}

});


/**
 * 语种判断接口
 * log_开头的变量为日志收集所用变量
 */
function get_lang_type(request){
	var log_starttime = new Date().getTime();//请求开始时间

	var value = request.lang_text;
	value = encodeURI(encodeURI(value));
	var sourceLang = "";
	$.ajax({
	  url: getSourceURL,
	  headers: {
            "access_token":getResult()
      },
	  type: 'POST',
	  async: false,//同步
	   dataType: 'json',
	  data: {s:value},
	  success: function(data) {
	    //called when successful
	    if(data != null && data.code == 1){
	    	sourceLang = data.data;
	    	//日志记录
			var log_endtime = new Date().getTime();//请求结束时间
		  	var log_sec = log_endtime - log_starttime;//请求耗时
		  	var log_website = request.website;//网站
		  	var log_weburl = request.weburl;//网址
		  	var log_num = 1;//请求次数
		  	var log_web_lang = data.data;//源语言(网页语言)
		  	var log_sys_lang = localStorage['log_common_info_browser_lang'];//系统语言(浏览器的语言)
			localStorage["log_upload_lang_judge_"+(new Date().getTime())] = 
			"03|"+get_log_head_info()+"|"+log_starttime+"|"+log_endtime+"|"+log_sec+"|"+log_website+"|"+log_weburl+"|"+log_num+"|"+log_web_lang+"|"+log_sys_lang;
	    }
	    
	  }
	});
   return (sourceLang == undefined || sourceLang == "")? "en" : sourceLang;
}


/**
 * 划词翻译接口
 * log_开头的变量为日志收集所用变量
 */
function translate(request){
	var translateId = request.translateId;

	var sour = request.sour;
	var tar  = request.tar;
	var text = request.text;
	
	var response = "";

	var data_dict = {"srcl":sour,"tgtl":tar,"word":text};
	
	var log_starttime = new Date().getTime();//开始时间
	$.ajax({
	  url: transURL,
	  headers: {
            "access_token":getResult()
      },
	  type: 'POST',
	  async: false,
	  dataType: 'json',
	  data: data_dict,
	  success: function(data) {

	    //called when successful
	    if(data != null){
			response = data.data;

			//文字翻译 - 日志记录
			var log_endtime = new Date().getTime();//结束时间
			var log_sec = log_endtime - log_starttime;//耗时
		  	var log_website = request.website;//网站
		  	var log_weburl = request.weburl;//网址
		  	var log_trans_type = "2";//1全文2划词3网页
		  	var log_translate_id = translateId;//翻译id
		  	var log_sour_lang = sour;//源语言
		  	var log_tar_lang = tar;//目标语言
		  	var log_text_length = text.length;//字数
		  	localStorage["log_upload_word_trans_"+(new Date().getTime())] = 
		  	"04|"+get_log_head_info()+"|"+log_starttime+"|"+log_endtime+"|"+log_sec+"|"+log_website+"|"+log_weburl+"|"+
		  	log_trans_type+"|"+log_translate_id+"|"+log_sour_lang+"|"+log_tar_lang+"|"+log_text_length;
	    }
	    
	  }
	});
	

	return response;
	
}

/**
 * 网页批量翻译接口
 */
function list_translate(request){
	var translateId = request.translateId;


	var translation_result = {};

	var src = request.src;
	var tgt = request.tgt;
	var texts = request.texts;
	
	var text_length = texts.length;
	
	var log_starttime = new Date().getTime();//开始时间
	$.ajax({
	  url: transURL,
	  headers: {
            "access_token":getResult()
      },
	  type: 'POST',
	  dataType: 'json',
	  async:false,
	  traditional: true, 
	  data:{
	  	srcl : src,
	  	tgtl : tgt,
	  	word : texts
	  },
	  success: function(data) {
			translation_result = data;


			//文字翻译 - 日志记录
			var log_endtime = new Date().getTime();//结束时间
			var log_sec = log_endtime - log_starttime;//耗时
		  	var log_website = request.website;//网站
		  	var log_weburl = request.weburl;//网址
		  	var log_trans_type = "1";//1全文2划词3网页
		  	var log_translate_id = translateId;//翻译id
		  	var log_sour_lang = src;//源语言
		  	var log_tar_lang = tgt;//目标语言
		  	var log_text_length = text_length;//字数
		  	localStorage["log_upload_word_trans_"+(new Date().getTime())] = 
		  	"04|"+get_log_head_info()+"|"+log_starttime+"|"+log_endtime+"|"+log_sec+"|"+log_website+"|"+log_weburl+"|"+
		  	log_trans_type+"|"+log_translate_id+"|"+log_sour_lang+"|"+log_tar_lang+"|"+log_text_length;

		
	  }
	 //  error: function(xhr, textStatus, errorThrown) {
		// var frm = document.getElementById("yeetool");
		// frm.contentWindow.postMessage({
		// action : "YEEKIT_NOT_SUPPORT"
		// }, "*");
	 //  }
	});

	return translation_result;
}